package com.mindbox.mudx

import android.content.Context
import android.content.res.Configuration
import android.os.Bundle
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.SurfaceHolder

class XLoopHolder(id: String) {
    companion object {
        init {
            System.loadLibrary("xloop")
        }
    }

    private var mNative: Long = 0
    private var mID = id

    fun onCreate(ctx: Context, savedInstanceState: Bundle?) {
        mNative = onCreateNative(mID, ctx, savedInstanceState)
    }

    fun onDestroy() {
        onDestroyNative(mNative)
        mNative = 0
    }

    fun onStart() {
        onStartNative(mNative)
    }

    fun onStop() {
        onStopNative(mNative)
    }

    fun onResume() {
        onResumeNative(mNative)
    }

    fun onPause() {
        onPauseNative(mNative)
    }

    fun surfaceCreated(holder: SurfaceHolder) {
        surfaceCreatedNative(mNative, holder)
    }

    fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        surfaceChangedNative(mNative, holder, format, width, height)
    }

    fun surfaceDestroyed(holder: SurfaceHolder) {
        surfaceDestroyedNative(mNative, holder)
    }

    fun surfaceRedrawNeeded(holder: SurfaceHolder) {
        surfaceRedrawNeededNative(mNative, holder)
    }

    fun onConfigurationChanged(newConfig: Configuration) {
        onConfigurationChangedNative(mNative, newConfig)
    }

    fun onKey(event: KeyEvent): Boolean {
        return onKeyNative(mNative, event)
    }

    fun onTouch(event: MotionEvent): Boolean {
        return onTouchNative(mNative, event)
    }

    fun onCommitEnd(data: String) {
        onCommitEndNative(mNative, data)
    }

    fun onCommitBack() {
        onCommitBackNative(mNative)
    }

    private external fun onCreateNative(id: String, ctx: Context, savedInstanceState: Bundle?): Long
    private external fun onDestroyNative(key: Long)
    private external fun onStartNative(key: Long)
    private external fun onStopNative(key: Long)
    private external fun onResumeNative(key: Long)
    private external fun onPauseNative(key: Long)

    private external fun surfaceCreatedNative(key: Long, holder: SurfaceHolder)
    private external fun surfaceChangedNative(key: Long, holder: SurfaceHolder, format: Int, width: Int, height: Int)
    private external fun surfaceDestroyedNative(key: Long, holder: SurfaceHolder)
    private external fun surfaceRedrawNeededNative(key: Long, holder: SurfaceHolder)

    private external fun onConfigurationChangedNative(key: Long, newConfig: Configuration)

    private external fun onKeyNative(key: Long, event: KeyEvent): Boolean
    private external fun onTouchNative(key: Long, event: MotionEvent): Boolean
    private external fun onCommitEndNative(key: Long, data: String)
    private external fun onCommitBackNative(key: Long)

}